/*
 * Powered By 
 * Since 2010 - 2016
 */

package com.rf.business.control;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.rf.sys.annotation.SystemLog;
import com.rf.sys.frm.BaseController;
import com.rf.sys.model.Dictionary;
import com.rf.sys.model.User;
import com.rf.sys.service.CommonService;
import com.rf.sys.utils.ConfigUtils;
import com.rf.sys.utils.JsonUtils;
import com.rf.sys.utils.PageUtils;
import com.rf.sys.utils.UuidUtil;
import com.rf.business.model.CarDriverArchives;
import com.rf.business.service.CarDriverArchivesService;

@Controller
@RequestMapping("/bus/carDriverArchives")
public class CarDriverArchivesController extends BaseController{
	/**
	 * 日志
	 */
	private static Logger log = LoggerFactory.getLogger(CarDriverArchivesController.class);
	private static final long serialVersionUID = -120161117110150937L;
	
	@Resource
	private CarDriverArchivesService carDriverArchivesService;
	@Resource
	private CommonService commonService;
	
	/**
	 * 初始化页面信息
	 * @param request,response
	 * @return ModelAndView
	 * @throws ServletException,IOException
	 */
	@RequestMapping(value = "index")
    public ModelAndView index (HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("/business/carDriverArchives/list");
		return modelAndView; 
    }
    
    /**
	 * 搜索页面信息
	 * @param Page,request,response
	 * @return JSON
	 * @throws ServletException,IOException
	 */
    @RequestMapping(value = "query")
    @ResponseBody
    public void query (PageUtils pageUtils, CarDriverArchives carDriverArchives, HttpServletRequest request, HttpServletResponse response ) throws Exception{
    	//获取分页、查询参数，进行数据封装
    	Map searchMap = carDriverArchivesService.getCarDriverArchivesPage(carDriverArchives, pageUtils);
    	super.responsePrint(request, response, JsonUtils.mapToJson(searchMap));
    }
    
    /**
     * 编辑页面初始化
     * @param request,response
	 * @return ModelAndView
	 * @throws ServletException,IOException
     */
    @RequestMapping(value = "edit")
    public ModelAndView edit (HttpServletRequest request, HttpServletResponse response ) throws Exception{
    	ModelAndView modelAndView = new ModelAndView();
		String id = request.getParameter("id");
		CarDriverArchives carDriverArchives = new CarDriverArchives();
		if(id != null && !id.equals("")){
			carDriverArchives = carDriverArchivesService.getCarDriverArchivesByID(id);	
		}else{
//			carDriverArchives.setState(ConfigUtils.ENABLESTATE);
		}
		modelAndView.addObject("carDriverArchives", carDriverArchives);
        modelAndView.setViewName("/business/carDriverArchives/edit");
        return modelAndView; 
    }
    
    /**
     * 保存数据信息
     * @param CarDriverArchives,request,response
	 * @return ModelAndView
	 * @throws ServletException,IOException
     */
    @RequestMapping(value = "save")
    @SystemLog(module="业务功能",methods="车辆备案管理-保存驾驶员")
    public void save (CarDriverArchives carDriverArchives, HttpServletRequest request, HttpServletResponse response ) throws Exception{
    	String ret = "success";
		if(carDriverArchives !=null && carDriverArchives.getId()!=null && !carDriverArchives.getId().equals("")){
			//修改
			carDriverArchivesService.updateCarDriverArchives(carDriverArchives);
		}else{
			//新增
			CarDriverArchives cda = new CarDriverArchives();
			cda.setCaId(carDriverArchives.getCaId());
			cda.setPassportNo(carDriverArchives.getPassportNo());
			List<CarDriverArchives> cdaList = carDriverArchivesService.getCarDriverArchives(cda);
			if(cdaList !=null && cdaList.size()>0){
				ret  = "repeat";//护照信息已存在
			}else{
			carDriverArchives.setId(UuidUtil.get32UUID());
			carDriverArchives.setArchiveNo(commonService.getSerialNo(3));
			carDriverArchivesService.addCarDriverArchives(carDriverArchives);
			}
		}
		super.resPrint(request, response, ret);//成功 
    }
    
    /**
     * 删除数据信息
     * @param request,response
	 * @return JSON
	 * @throws ServletException,IOException
	 */
    @RequestMapping(value = "delete")
    @ResponseBody
    @SystemLog(module="业务功能",methods="车辆备案管理-删除驾驶员")
    public void delete (HttpServletRequest request, HttpServletResponse response) throws Exception{
    	JSONObject returnObj = new JSONObject();
    	String ids = request.getParameter("id");
    	carDriverArchivesService.deleteCarDriverArchives(ids);
		returnObj.element("message", "success");//成功
		super.responseJson(request, response, returnObj.toString());
    }
    
    /**
     * 更新数据状态
     * @param carDriverArchives,request,response
	 * @return JSON
	 * @throws ServletException,IOException
	 */
    @RequestMapping(value = "updateState")
    @ResponseBody
    @SystemLog(module="业务功能",methods="车辆备案管理-更新驾驶员状态")
    public void updateState (CarDriverArchives carDriverArchives, HttpServletRequest request, HttpServletResponse response) throws Exception{
    	JSONObject returnObj = new JSONObject();
    	carDriverArchivesService.updateCarDriverArchives(carDriverArchives);
		returnObj.element("message", "success");//成功
		super.responseJson(request, response, returnObj.toString());
    }
    
    
    /**
     * 根据车辆备案号获取驾驶员下拉框--申报单管理编辑页面使用
     * @author yangxiao
     * @param request
     * @param response
     * @throws Exception 
     */
	@RequestMapping(value = "driverbykey", method = RequestMethod.GET)
	@ResponseBody
	public void queryDriverbykey(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String key=request.getParameter("key");
		List<CarDriverArchives> cdaList = carDriverArchivesService.getCarDriverArchivesByArchiveNo(key);
		CarDriverArchives cda1 = new CarDriverArchives();
		cda1.setId("");
		cda1.setName("请选择...");
		cdaList.add(cda1);
		
		JSONArray jsonarray = JSONArray.fromObject(cdaList);  
		responsePrint(request,response, jsonarray.toString());
	}
	
	/**
     * 查询驾驶员护照号--申报单管理编辑页面使用
     * @author yangxiao
     * @param request,response
	 * @throws IOException
	 */
    @RequestMapping(value = "driverpass")
    @ResponseBody
    public void queryDriverPass (HttpServletRequest request, HttpServletResponse response) throws Exception{
    	JSONObject returnObj = new JSONObject();
    	String id = request.getParameter("id");
    	CarDriverArchives carDriverArchives = carDriverArchivesService.getCarDriverArchivesByID(id);
    	returnObj.element("passportNo", carDriverArchives.getPassportNo());
		returnObj.element("message", "success");//成功
		super.responseJson(request, response, returnObj.toString());
    }
}


